using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 内容模型管理接口。
    /// 提供内容模型的增删改查及字段类型查询等 API。
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ContentModelController : ControllerBase
    {
        /// <summary>
        /// 内容服务应用层接口
        /// </summary>
        private readonly IContentAppService _service;

        /// <summary>
        /// 构造函数，注入内容服务
        /// </summary>
        /// <param name="service">内容服务应用层接口</param>
        public ContentModelController(IContentAppService service)
        {
            _service = service;
        }
        /// <summary>
        /// 获取所有内容模型
        /// </summary>
        /// <returns>内容模型列表</returns>
        [HttpGet]
        public async Task<IEnumerable<ContentModelDto>> GetAll()
        {
            return await _service.GetAllModelsAsync();
        }
        /// <summary>
        /// 根据ID获取内容模型
        /// </summary>
        /// <param name="id">内容模型主键</param>
        /// <returns>内容模型数据</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<ContentModelDto>> GetById(long id)
        {
            var result = await _service.GetModelByIdAsync(id);
            if (result == null) return NotFound();
            return result;
        }
        /// <summary>
        /// 添加内容模型
        /// </summary>
        /// <param name="dto">内容模型数据传输对象</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<IActionResult> Add(ContentModelDto dto)
        {
            await _service.CreateModelAsync(dto);
            return Ok();
        }
        /// <summary>
        /// 更新内容模型
        /// </summary>
        /// <param name="id">内容模型主键</param>
        /// <param name="dto">内容模型数据传输对象</param>
        /// <returns>操作结果</returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(long id, ContentModelDto dto)
        {
            await _service.UpdateModelAsync(id, dto);
            return Ok();
        }
        /// <summary>
        /// 删除内容模型
        /// </summary>
        /// <param name="id">内容模型主键</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(long id)
        {
            await _service.DeleteModelAsync(id);
            return Ok();
        }

        // 查询支持的字段类型
        /// <summary>
        /// 获取支持的字段类型
        /// </summary>
        /// <returns>字段类型数组</returns>
        [HttpGet("field-types")]
        public IActionResult GetSupportedFieldTypes()
        {
            var types = new[] { "text", "number", "boolean", "date", "select" };
            return Ok(types);
        }
    }
}
